package com.example.smartcity1.my;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.example.smartcity1.R;
import com.example.smartcity1.bean.MsgCodeBean;
import com.example.smartcity1.bean.MyUpDataPostBean;
import com.example.smartcity1.bean.MyUserInfoBean;
import com.example.smartcity1.network.RetrofitClient;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.example.smartcity1.network.RetrofitClient.IP;

public class MyUserInfoActivity extends AppCompatActivity {

    private ImageView myUserInfoImg;
    private RadioGroup myUserInfoSex;
    private RadioButton myUserInfoSex1;
    private RadioButton myUserInfoSex0;
    private EditText myUserInfoNickName;
    private EditText myUserInfoPhone;
    private TextView myUserInfoUserName;
    private TextView myUserInfoNumber;
    private TextView myUserInfoTime;
    private Button button;

    private SharedPreferences user;
    private String token;
    private String sex = "1";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_user_info);
        initView();
        user = getSharedPreferences("user", MODE_PRIVATE);
        token = user.getString("token","");
        setViewData();
        myUserInfoSex.setOnCheckedChangeListener(((group, checkedId) -> {
            switch (checkedId){
                case R.id.sex_1_radio:
                    sex = "1";
                    break;
                case R.id.sex_0_radio:
                    sex = "0";
                    break;
            }
        }));
        button.setOnClickListener(v -> {
            MyUpDataPostBean myUpDataPostBean = new MyUpDataPostBean(myUserInfoNickName.getText().toString(),myUserInfoNumber.getText().toString(),sex);
            RetrofitClient.appService().getMyUSerUpData(token,myUpDataPostBean).enqueue(new Callback<MsgCodeBean>() {
                @Override
                public void onResponse(Call<MsgCodeBean> call, Response<MsgCodeBean> response) {
                    MsgCodeBean msgCodeBean = response.body();
                    if (msgCodeBean != null) {
                        if (msgCodeBean.getCode() == 200) {
                            Toast.makeText(MyUserInfoActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(MyUserInfoActivity.this, ""+msgCodeBean.getMsg(), Toast.LENGTH_SHORT).show();
                        }
                    }
                }

                @Override
                public void onFailure(Call<MsgCodeBean> call, Throwable t) {

                }
            });
        });
        //设置返回键
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public boolean onSupportNavigateUp() {
        finish();//返回
        return super.onSupportNavigateUp();
    }

    private void initView() {
        myUserInfoImg = findViewById(R.id.my_user_info_img);
        myUserInfoSex = findViewById(R.id.sex_group);
        myUserInfoSex1 = findViewById(R.id.sex_1_radio);
        myUserInfoSex0 = findViewById(R.id.sex_0_radio);
        myUserInfoNickName = findViewById(R.id.name_edit);
        myUserInfoPhone = findViewById(R.id.phone_edit);
        myUserInfoUserName = findViewById(R.id.my_user_info_user_name);
        myUserInfoNumber = findViewById(R.id.my_user_info_number);
        myUserInfoTime = findViewById(R.id.my_user_info_time);
        button = findViewById(R.id.button);
    }

    private void setViewData() {
        RetrofitClient.appService().getMyUserInfoData(token).enqueue(new Callback<MyUserInfoBean>() {
            @Override
            public void onResponse(Call<MyUserInfoBean> call, Response<MyUserInfoBean> response) {
                MyUserInfoBean myUserInfoBean = response.body();
                if (myUserInfoBean != null && myUserInfoBean.getCode() == 200) {
                    MyUserInfoBean.UserDTO user = myUserInfoBean.getUser();
                    if (!user.getAvatar().equals("")) {
                        Glide.with(MyUserInfoActivity.this)
                                .load(IP + user.getAvatar())
                                .into(myUserInfoImg);
                    }
                    if (user.getSex().equals("1")) {
                        sex = "1";
                        myUserInfoSex1.setChecked(true);
                    } else {
                        sex = "0";
                        myUserInfoSex0.setChecked(false);
                    }
                    myUserInfoNickName.setText(user.getNickName());
                    myUserInfoPhone.setText(user.getPhonenumber());
                    myUserInfoUserName.setText(user.getUserName());
                    if (user.getIdCard() != null) {
                        myUserInfoNumber.setText(user.getIdCard().toString());
                    } else {
                        myUserInfoNumber.setText("未上传");
                    }
                    myUserInfoTime.setText(user.getCreateTime());
                }
            }

            @Override
            public void onFailure(Call<MyUserInfoBean> call, Throwable t) {

            }
        });
    }

}